<?php

	class fluxUri {

		protected $parsedURI	= array ();
		protected $uriDelimiter	= NULL;
		protected $documentRoot	= NULL;

		protected function isUserDir () {

			$uri = explode ('/',$_SERVER['REQUEST_URI']);
			$uri = $uri[1];

			if (preg_match ('#^~#',$uri))
				return ($uri);

			return FALSE;
		}

		protected function parseUri() {

			$actionController   = preg_replace('#(index\.php)$#','',$_SERVER['SCRIPT_NAME']);
			$this->documentRoot = preg_replace ('#^\/|\/$#','',$actionController); 
			$actionController   = substr($_SERVER['REQUEST_URI'],strlen($actionController));
			$uri                = $actionController;

			// Expresion regular del voto peronista
			$temp = preg_replace ('#^\/|\/$#','',$actionController); 

			$temp = NULL;

			$temp = explode ($this -> uriDelimiter,$actionController);

			//$controller = $temp[0];
			//$action     = $temp[1];


			//The default action and controller is indexController and indexAction
			//However if this was overriden by the user ...

			$defCon = (is_null ($this -> defController)) ? 'index' : $this -> defController;
			$defAct = (is_null ($this -> defAction))     ? 'index' : $this -> defAction;

			$controller = (is_null($temp[0])||empty($temp[0])) ? $defCon : $temp[0];
			$action     = (!isset($temp[1]) ||(empty($temp[1])||is_null($temp[1])))  ? $defAct : $temp[1];

			$parameters = $this -> getActionParameters($uri,$controller,$action);

			$this -> parsedURI = array (
												'documentRoot' => '/' . $this -> documentRoot,
												'controller'   => $controller,
												'action'       => $action,
												'parameters'   => $parameters
									 );


			return;	

		}

		public function getActionParameters ($uri=NULL,$controller=NULL,$action=NULL) {

			if (is_null ($uri) || is_null ($controller) || is_null ($action))
				return NULL;

			$parameters = NULL;
			$params     = array();

			$parameters = substr ($uri,strlen($action)+strlen($controller)+2);

			if ($parameters==FALSE)
					  return;

			$parameters = explode ($this -> uriDelimiter,$parameters);


			while (list ($k,$v) = each  ($parameters)) {

				if(array_key_exists($k+1,$parameters)){
					if(!$parameters[$k+1]){
						$params[$v] = NULL;
						continue;
					}
				}

				if(array_key_exists($k+1,$parameters))
						  $params[$v] = $parameters[$k+1];
				else
						  $params[$v] = $parameters[$k];

				if (!preg_match('#^[a-z]|[A-Z]#',$parameters[$k]))
						  continue;

				//throw (new fluxXception ('Invalid paremeter specified'));

				next ($parameters);


			}

			return ($params);

		}

		public function getUriDelimiter ($delimiter=NULL) {
    
			return ($this -> uriDelimiter);
    
		}

		public function setUriDelimiter ($delimiter=NULL) {
    
			if (is_null($delimiter) || empty ($delimiter)) {

				$this -> uriDelimiter = '/';
				return;	

			}
				

			$this -> uriDelimiter = $delimiter;

			return;

		}


	}

?>
